package com.moonbird.springboot.Utils.Qwen;

import java.util.ArrayList;
import java.util.List;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;

public class QwenTurboUtil {

    private static final String API_KEY = "sk-c91bee81e9ef4fd4abe43a61b8f38479";
    
    static {
        Constants.apiKey = API_KEY;
    }

    public static GenerationParam createGenerationParam(List<Message> messages) {
        return GenerationParam.builder()
                .model("qwen-turbo")
                .messages(messages)
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .topP(0.8)
                .build();
    }

    public static GenerationResult callGenerationWithMessages(GenerationParam param) throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        return gen.call(param);
    }

    public static Message createMessage(Role role, String content) {
        return Message.builder().role(role.getValue()).content(content).build();
    }

    public static String generateResponse(List<Message> messages) throws ApiException, NoApiKeyException, InputRequiredException {
        GenerationParam param = createGenerationParam(messages);
        GenerationResult result = callGenerationWithMessages(param);
        return result.getOutput().getChoices().get(0).getMessage().getContent();
    }
}